package richbar.com.richbar.wxapi;
/**
 * Created by Administrator on 2018/12/21.
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import richbar.com.richbar.activity.product.FirmOrderAc;
import richbar.com.richbar.activity.product.FirmOrderShopCartAc;
import richbar.com.richbar.activity.product.OrderCompleteAc;
import richbar.com.richbar.adapter.order.OrderAdapter;
import richbar.com.richbar.help.Constants;
import richbar.com.richbar.util.NetWorkUtil;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0)//支付成功
            {
                String orderid = "";
                if (FirmOrderShopCartAc.bln_kind) {
                    OrderCompleteAc.start(WXPayEntryActivity.this, FirmOrderShopCartAc.Orderid);
                    FirmOrderShopCartAc.bln_kind = false;
                    orderid = FirmOrderShopCartAc.Orderid;
                }
                if (FirmOrderAc.bln_kind) {
                    OrderCompleteAc.start(WXPayEntryActivity.this, FirmOrderAc.Orderid);
                    FirmOrderAc.bln_kind = false;
                    orderid = FirmOrderAc.Orderid;
                }
                if (OrderAdapter.bln_kind) {
                    OrderCompleteAc.start(WXPayEntryActivity.this, OrderAdapter.Orderid);
                    OrderAdapter.bln_kind = false;
                    orderid = OrderAdapter.Orderid;
                }

                NetWorkUtil.setPay(orderid, new HttpUtils.HttpCallback() {
                    @Override
                    public void onSuccess(String data) {

                    }

                    @Override
                    public void onError(String msg) {
                        super.onError(msg);
                        ToastUtil.showToast(WXPayEntryActivity.this, msg);
                    }
                });


                finish();
                ToastUtil.showToast(WXPayEntryActivity.this, "支付成功");
            }
            if (resp.errCode == -2)//用户取消
            {
                finish();
                FirmOrderShopCartAc.bln_kind = false;
                FirmOrderAc.bln_kind = false;
                OrderAdapter.bln_kind=false;
                ToastUtil.showToast(WXPayEntryActivity.this, "用户取消");
            }
            if (resp.errCode == -1)//支付出错
            {
                FirmOrderShopCartAc.bln_kind = false;
                FirmOrderAc.bln_kind = false;
                OrderAdapter.bln_kind=false;
                finish();
                ToastUtil.showToast(WXPayEntryActivity.this, "用户取消");
            }
        }
    }
}


